import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
  {
    path: '',
    redirect: '/index',
    meta: {
      title: '', // 通过meta动态修改html页面的title
      keepAlive: true
    }
  },
  // 首页
  {
    path: '/index',
    name: 'index',
    component: ()=> import("../views/index.vue"),
    meta: {
      keepAlive: true
    }
  },
  // 公告列表
  {
    path: '/noticeList',
    name: 'noticeList',
    component: ()=> import("../views/Notice/List.vue"),
  },
  // 公告详情
  {
    path: '/noticeDetails',
    name: 'noticeDetails',
    component: ()=> import("../views/Notice/Details.vue"),
  },
  // 邀请码
  {
    path: '/invitationCode',
    name: 'invitationCode',
    component: ()=> import("../views/InvitationCode/index.vue"),
  },
  // 我的团队
  {
    path: '/team',
    name: 'team',
    component: ()=> import("../views/Team/index.vue"),
  },
  // 资产
  {
    path: '/property',
    name: 'property',
    component: ()=> import("../views/Property/index.vue"),
  },
  // 认购记录
  {
    path: '/subscriptionRecord',
    name: 'subscriptionRecord',
    component: ()=> import("../views/SubscriptionRecord/index.vue"),
  },
  // 收益详情
  {
    path: '/revenueDetails',
    name: 'revenueDetails',
    component: ()=> import("../views/RevenueDetails/index.vue"),
  },
  // 质押
  {
    path: '/pledge',
    name: 'pledge',
    component: ()=> import("../views/Pledge/index.vue"),
  },
]

// 创建路由实例
const router = createRouter({
  history: createWebHashHistory(), // createWebHashHistory URL带#，createWebHistory URL不带#
  strict: true,
  routes,
  // scrollBehavior: () => ({ left: 0, top: 0 })
})

router.beforeEach((to, from, next) => {
  if (to?.meta?.title) {
    document.title = to.meta.title
  }
  next()
})

export const resetRouter = () => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app) => {
  app.use(router)
}
window.router = router
export default router
